package top.coolrainly.module.system.controller.app.auth;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.coolrainly.framework.common.pojo.CommonResult;
import top.coolrainly.framework.common.util.object.BeanUtils;
import top.coolrainly.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import top.coolrainly.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import top.coolrainly.module.system.controller.app.auth.vo.AppAuthLoginReqVO;
import top.coolrainly.module.system.controller.app.auth.vo.AppMemberUserRespVO;
import top.coolrainly.module.system.service.auth.AdminAuthService;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static top.coolrainly.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 认证")
@RestController
@RequestMapping("/system/auth")
@Validated
@Slf4j
public class AppAuthController {

    @Resource
    private AdminAuthService authService;

    @PostMapping("/login")
    @PermitAll
    @Operation(summary = "使用账号密码登录")
    public CommonResult<AuthLoginRespVO> login(@RequestBody @Valid AppAuthLoginReqVO reqVO) {
        return success(authService.appLogin(reqVO));
    }

    @PostMapping("/refresh-token")
    @PermitAll
    @Operation(summary = "刷新令牌")
    @Parameter(name = "refreshToken", description = "刷新令牌", required = true)
    public CommonResult<AuthLoginRespVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
        return success(authService.refreshToken(refreshToken));
    }

    @GetMapping("/get-info")
    @Operation(summary = "获取用户信息")
    public CommonResult<AppMemberUserRespVO> getUserInfo() {
        return success(BeanUtils.toBean(authService.getAppMemberUser(), AppMemberUserRespVO.class));
    }
}
